package com.monitor.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 各种id生成策略
 * <p>Title: IDUtils</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.com</p> 
 * @author	入云龙
 * @date	2015年7月22日下午2:32:10
 * @version 1.0
 */
public class IDUtils {
	//用户前缀
	private static final String PREFIX_USER = "U";
    //商品前缀
    private static final String PREFIX_ITEMID = "I";
    //订单前缀
    private static final String PREFIX_ORDERID = "O";
    //短信验证码前缀
    private static final String PREFIX_CODE = "C";
  //短信验证码前缀
    private static final String PREFIX_INFOR_CODE = "Z";
    
    private static final String PREFIX_COPY = "COPY";
    private static final String PREFIX_ITEM = "ITEM";
    /**
	 * 生成用户id
	 */
	public static String genUserId() {
		//取当前时间的长整形值包含毫秒
		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上三位随机数
		Random random = new Random();
		int end3 = random.nextInt(999);
		//如果不足三位前面补0
		String str = PREFIX_USER + millis + String.format("%03d", end3);
		return str;
	}
	/**
	 * 图片名生成
	 */
	public static String genImageName() {
		//取当前时间的长整形值包含毫秒
		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上三位随机数
		Random random = new Random();
		int end3 = random.nextInt(999);
		//如果不足三位前面补0
		String str = millis + String.format("%03d", end3);
		
		return str;
	}
	
	/**
	 * 商品id生成
	 */
	public static String genItemId() {
		//取当前时间的长整形值包含毫秒
		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上两位随机数
		Random random = new Random();
		int end2 = random.nextInt(99);
		//如果不足两位前面补0
		String str = PREFIX_ITEMID + millis + String.format("%02d", end2);
		return str;
	}
	/**
	 * 订单id生成
	 *
	 * 单号生成
	 * 订单号生成
	 */
	public static String genOrderId() {

		Date date=new Date();
		//设置要获取到什么样的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		//获取String类型的时间
		String createdate = sdf.format(date);


		//取当前时间的长整形值包含毫秒

		//long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上两位随机数
		/*Random random = new Random();
		int end2 = random.nextInt(99);
		//如果不足两位前面补0
		String str = PREFIX_ORDERID + millis + String.format("%02d", end2);*/
		//long id = new Long(str);
//		return millis+"";
	return  createdate;
	}

	public static String gennianyueri() {

		Date date=new Date();
		//设置要获取到什么样的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//获取String类型的时间
		String createdate = sdf.format(date);


		//取当前时间的长整形值包含毫秒

		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上两位随机数
		/*Random random = new Random();
		int end2 = random.nextInt(99);
		//如果不足两位前面补0
		String str = PREFIX_ORDERID + millis + String.format("%02d", end2);*/
		//long id = new Long(str);
//		return millis+"";
		return  createdate;
	}
	/**
	 * 咨询id生成
	 */
	public static String genInformationId() {
		//取当前时间的长整形值包含毫秒
		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上两位随机数
		Random random = new Random();
		int end2 = random.nextInt(99);
		//如果不足两位前面补0
		String str = PREFIX_INFOR_CODE + millis + String.format("%02d", end2);
		return str;
	}

	/**
	 * 字符串+1方法，该方法将其结尾的整数+1,适用于任何以整数结尾的字符串,不限格式，不限分隔符。
	 * @author zcz
	 * @param testStr 要+1的字符串
	 * @return +1后的字符串
	 * @exception NumberFormatException
	 */
	public static String addOne(String testStr){
		String[] strs = testStr.split("[^0-9]");//根据不是数字的字符拆分字符串
		String numStr = strs[strs.length-1];//取出最后一组数字
		if(numStr != null && numStr.length()>0){//如果最后一组没有数字(也就是不以数字结尾)，抛NumberFormatException异常
			int n = numStr.length();//取出字符串的长度
			int num = Integer.parseInt(numStr)+1;//将该数字加一
			String added = String.valueOf(num);
			n = Math.min(n, added.length());
			//拼接字符串
			return testStr.subSequence(0, testStr.length()-n)+added;
		}else{
			throw new NumberFormatException();
		}
	}
	
	/**
	 * 验证码token
	 */
	public static String genCodeToken() {
		//取当前时间的长整形值包含毫秒
		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上两位随机数
		Random random = new Random();
		int end2 = random.nextInt(99);
		//如果不足两位前面补0
		String str = PREFIX_CODE + millis + String.format("%02d", end2);
		//long id = new Long(str);
		return str;
	}
	public static String genCopyId() {
		//取当前时间的长整形值包含毫秒
		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上两位随机数
		Random random = new Random();
		int end2 = random.nextInt(99);
		//如果不足两位前面补0
		String str = PREFIX_COPY + millis + String.format("%02d", end2);
		//long id = new Long(str);
		return str;
	}
	public static String genItemCartId() {
		//取当前时间的长整形值包含毫秒
		long millis = System.currentTimeMillis();
		//long millis = System.nanoTime();
		//加上两位随机数
		Random random = new Random();
		int end2 = random.nextInt(99);
		//如果不足两位前面补0
		String str = PREFIX_ITEM + millis + String.format("%02d", end2);
		//long id = new Long(str);
		return str;
	}

    public static String genSixNum() {
        Random random = new Random();
        String end2 = random.nextInt(100001)+"";
        return end2;
    }

	public static void main(String[] args) {

		System.out.println(addOne("aaaa20181106a00001"));
		//for(int i=0;i< 100;i++)
//		System.out.println(genUserId());
//
//		BigDecimal b1 = new BigDecimal(Double.toString(0.88));
//		BigDecimal b2 = new BigDecimal(Double.toString(0.59));
//		System.out.println(b1.add(b2).doubleValue());
            System.out.println(genOrderId());

	}
}
